package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.supervision.CheckItem;
import com.example.entity.supervision.PlanDictItem;
import com.example.mapp.PlanDictItemMapp;
import com.example.service.PlanDictItemService;
import com.example.util.ResultUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by simon on 2018/8/9.
 */
@Service
public class PlanDictItemServiceImpl implements PlanDictItemService {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(PlanDictItemServiceImpl.class);


    @Autowired
    PlanDictItemMapp planDictItemMapp;

    @Override
    public Map<String, Object> query(Map map) {
        Map<String, Object> map1 = new HashMap<>();
       try {
           List<CheckItem> list = planDictItemMapp.query(map);
           List<Map<String, Object>> dolist = new ArrayList();
           for (CheckItem checkItem : list ) {
               if(list.size() == 0){
                   Map<String,Object> mapGrapa = new HashMap();
                   Map<String,Object> mapFather = new HashMap();
                   Map<String,Object> mapSon = new HashMap<>();

                   mapSon.put("itemContent",checkItem.getItemContent());
                   mapSon.put("id",checkItem.getId());

                   List<Map> itemList = new ArrayList();
                   itemList.add(mapSon);

                   mapFather.put("itemName",checkItem.getItemName());
                   mapFather.put("itemContents",itemList);

                   List<Map> nameList = new ArrayList();
                   nameList.add(mapFather);

                   mapGrapa.put("itemType",checkItem.getItemType());
                   mapGrapa.put("nameContents",nameList);
                   dolist.add(mapGrapa);
               }else{
                   boolean isadd = false;
                   for (Map m: dolist ) {
                       if(checkItem.getItemType().equals(m.get("itemType")+"")){
                           Map<String,Object> map4 = new HashMap();
                           map4.put("itemContent",checkItem.getItemContent());
                           map4.put("id",checkItem.getId());
                           ((List)m.get("itemContents")).add(map4);
                           isadd = true;
                           break;
                       }
                   }
                   if(!isadd){
                       Map<String,Object> map2 = new HashMap();
                       Map<String,Object> map3 = new HashMap();
                       List<Map> list1 = new ArrayList();
                       map3.put("itemContent",checkItem.getItemContent());
                       map3.put("id",checkItem.getId());
                       list1.add(map3);
                       map2.put("itemName",checkItem.getItemName());
                       map2.put("itemContents",list1);
                       dolist.add(map2);
                   }

               }
           }
           map1.put("list",dolist);
           return ResultUtil.returnSuccess("success", map1);
       }catch(Exception e){
           logger.error(e.getMessage());
           return ResultUtil.returnError(e.getMessage(), null);
       }
    }

    /**
     * 用于查询所有的检查项。
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> queryPlanDictItem(Map map) {
        try {
            List<CheckItem> list = planDictItemMapp.query(map);
            map.put("list",list);
            return ResultUtil.returnSuccess("success", map);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(), null);
        }
    }
    @Override
    public Map<String, Object> add(Map<String, List<PlanDictItem>> map) {
        try {
            List<PlanDictItem> list = map.get("pdiList");
            planDictItemMapp.deleteByDictId(list.get(0));
            for(int i=0;i<list.size();i++) {
                planDictItemMapp.insert(list.get(i));
            }

            return ResultUtil.returnSuccess("success", null);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(), null);
        }
    }
}
